﻿using System;
using System.Collections.Generic;
using System.Text;
using Wjtour.Common;
using Wjtour.Domain;
using System.Linq;
using System.Data.Common;
using System.Data;

namespace Wjtour.BLL
{
    public class ad_bll : base_dll
    {
        public PaginationData<Wjtour.Domain.ad> GetList(string q, int page, int pagesize = 20)
        {
            string where = " where 1=1";
            if (!string.IsNullOrEmpty(q))
                where += " and title like '%" + Utility.GetSafeSql(q) + "%'";
            string sql = string.Format(@"select * from ad 
                {0} 
                order by id desc", where);
            PaginationData<Wjtour.Domain.ad> data = GetPaginationData<Wjtour.Domain.ad>(sql, page, pagesize);
            return data;
        }

        public Wjtour.Domain.ad Load(int id)
        {
            string sql = "select * from ad where id=" + id;
            return DbAccess.GetObjects<Wjtour.Domain.ad>(sql).FirstOrDefault();
        }

        public bool Save(Wjtour.Domain.ad entity)
        {
            string sql = "";

            DbCommand cmd = DbAccess.CreateCommand();

            if (entity.id > 0)
            {
                sql = @"UPDATE [ad] SET 
                    [img] = @img,
                    [link] = @link,
                    [title] = @title,
                    [height] = @height,
                    [width] = @width,
                    [content] = @content,
                    [adtype] = @adtype,
                    [addtime] = @addtime,
                    [sys] = @sys,
                    [edate] = @edate,
                    [type] = @type
                     WHERE id=@id";
                DbAccess.AddInParameter(cmd, "@id", DbType.Int32, entity.id);

            }
            else
            {
                sql = @"INSERT INTO [ad]( 
                [img],[link],[title],[height],[width],[content],[adtype],[addtime],[sys],[edate],[type])VALUES(
                @img,@link,@title,@height,@width,@content,@adtype,@addtime,@sys,@edate,@type)";
            }
            DbAccess.AddInParameter(cmd, "@img", DbType.String, entity.img);
            DbAccess.AddInParameter(cmd, "@link", DbType.String, entity.link);
            DbAccess.AddInParameter(cmd, "@title", DbType.String, entity.title);
            DbAccess.AddInParameter(cmd, "@height", DbType.String, entity.height);
            DbAccess.AddInParameter(cmd, "@width", DbType.String, entity.width);
            DbAccess.AddInParameter(cmd, "@content", DbType.String, entity.content);
            DbAccess.AddInParameter(cmd, "@adtype", DbType.String, entity.adtype);
            DbAccess.AddInParameter(cmd, "@addtime", DbType.DateTime, entity.addtime);
            DbAccess.AddInParameter(cmd, "@sys", DbType.Int16, entity.sys);
            DbAccess.AddInParameter(cmd, "@edate", DbType.DateTime, entity.edate);
            DbAccess.AddInParameter(cmd, "@type", DbType.Int16, entity.type);
            cmd.CommandText = sql;
            cmd.CommandType = System.Data.CommandType.Text;
            return DbAccess.ExecuteNonQuery(cmd) > 0;



        }

        public int BatchDelete(string ids)
        {
            string sql = "delete from ad where id in(" + ids + ")";
            return DbAccess.ExecuteNonQuery(sql);
        }
    }
}
